'use strict';
app.controller('merchantsCtrl',['$scope','$log','utilService','$modal', function($scope,$log,utilService,$modal){
    $scope.auditStatusObject=[
        {name:'待审核',value:'0'},
        {name:'审核不通过',value:'1'},
        {name:'审核通过',value:'2'},
    ];

    $scope.page={};
    $scope.page.currentPage=1;
    //分页
    $scope.replacePage = function () {
        $scope.currentPage= $scope.page.currentPage;
        $scope.pageChanged();
    };

    $scope.shop = {};
    // call server.
    $scope.query = function(){
        utilService.queryForPagingList($scope,{
            url:$scope.app.domain + 'shop',
            params:$scope.shop
        }).then(function(result){
        });
    }
    //变更查询商户类型
    $scope.changeType=function(type){
        $scope.list={};
        if(type==0){
            $scope.shop.isPlatform=0;
            $scope.shop.isChain=0;
            $scope.query();
        }
        if(type==1){
            $scope.shop.isPlatform=0;
            $scope.shop.isChain=1;
            $scope.query();
        }
        if(type==2){
            $scope.shop.isPlatform=1;
            $scope.shop.isChain=0;
            $scope.query();
        }
    }
    $scope.changeType(0);
    //更新状态
    $scope.openUpdateStatus = function (id,status,name) {
        var modalInstance = $modal.open({
            templateUrl: 'updateUserStatus.html',
            controller: 'updateUserStatusCtrl',
            scope:$scope,
            resolve: {
                data:function(){
                    var data={};
                    data.id=id;
                    data.status=status;
                    data.name=name;
                    return data;
                }
            }
        });

        modalInstance.result.then(function (result) {
            console.log(result);
        }, function () {
            $log.info('Modal dismissed at: ' + new Date());
        });

    };


    $scope.openMerchantsAudit = function(id, name,auditStatus,isSetType){
        if(isSetType!=0){
            var param = {
                id:id,
                name:name,
                auditStatus:auditStatus
            };
            // query data.
            utilService.modal($modal, $scope, {
                title:"审核",
                controller:"merchantsAuditCtrl",
                message:"你确定【" + name + "】 商户审核通过吗？",
                data:param
            });
        }else{
            var param = {
                id:id
            };
            utilService.modal($modal, $scope, {
                title:"提示",
                controller:"infoCtrl",
                message:"【" + name + "】请先至少勾选一种商户类型后才能通过审核",
                data:param
            });
        }
    }
}]);

app.controller('updateMerchantsCtrl',['$scope','utilService','merchantsService','toaster', function($scope,utilService,merchantsService,toaster){
    $scope.updateStatus = function (id,status) {
        merchantsService.updateSystemUserStatus($scope,id,status).then(function(result){
            switch(result.status){
                case 200:
                    toaster.pop('success', "提示", "操作成功");
                    $scope.query();
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    };
}]);


app.controller('merchantsViewCtrl',['$scope','merchantsService','utilService','$stateParams','toaster','$state','$filter','$window','$modal',
                            function($scope,merchantsService,utilService,$stateParams,toaster,$state,$filter,$window,$modal){
    //打开图片
    $scope.openUrl=function(url){
        $window.open(url);
    };

    $scope.getIndustryFullName=function(){
        merchantsService.getIndustryFullName($scope).then(function(result){
            switch (result.status){
                case 200:
                    $scope.data = result.data;
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }

        });
    };
    $scope.getIndustryFullName();

    $scope.shopUpdateVO={
        id : $stateParams.shopId,
        isOpenRebate:0,
        isOpenSell:0,
        isMail:0,
        isShop:0,
        isDelivery:0,
        isEat:0,
        isReserve:0
    };

    //判断是否已经选择服务类型
    $scope.check=function(){
        if($scope.shopUpdateVO.isShop==0 && $scope.shopUpdateVO.isMail == 0 && $scope.shopUpdateVO.isDelivery == 0 &&
            $scope.shopUpdateVO.isEat == 0 && $scope.shopUpdateVO.isReserve == 0 ){
            return true;
        }else{
            return false;
        }
    };

    //判断平台商家是否已经选择分销、返利类型
    $scope.checkType=function(){
        if($scope.shopUpdateVO.isPlatform==0 && $scope.shopUpdateVO.isOpenSell==0 && $scope.shopUpdateVO.isOpenRebate==0){
            return true;
        }else{
            return false;
        }
    };

    $scope.findById=function(){
        merchantsService.findById($scope).then(function(result){
            switch (result.status){
                case 200:
                    $scope.shopUpdateVO = result.data;
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    };

    $scope.findById();


    $scope.editMerchants = function(){
        if($scope.shopUpdateVO.expireTime != null){
            $scope.shopUpdateVO.expireTime = $filter("date")($scope.shopUpdateVO.expireTime, "yyyy-MM-dd");
            $scope.shopUpdateVO.expireTime=new Date($scope.shopUpdateVO.expireTime.replace(/-/g, "/"));
        }else{
            delete $scope.shopUpdateVO.expireTime;
        }
        merchantsService.updateMerchants($scope).then(function(result){
            switch (result.status){
                case 200:
                    toaster.pop('success', "提示", "操作成功");
                    $state.go("app.merchants");
                    break;
                case 604:
                    toaster.pop('error', "操作失败", "行业名称已存在");
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    };
    $scope.compare=function(){
        var now=new Date();
        if($scope.shopUpdateVO.expireTime!=null){
            var time=new Date($scope.shopUpdateVO.expireTime);
            if(time.getTime() < now.getTime()){
                return true;
            }
        }
        return false;
    };

    $scope.shopInfo={};
    $scope.findShopInfo=function(){
        merchantsService.findByShopId($scope).then(function(result){
            switch (result.status){
                case 200:
                    $scope.shopInfo=result.data;
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });
    };

    $scope.findShopInfo();


    $scope.selectSell = function(isOpenSell){
        if(isOpenSell == 0){
            var param = {
                isOpenSell: isOpenSell
            };
            // query data.
            utilService.modal($modal, $scope, {
                title: "警告",
                controller: "updateSellCtrl",
                message: "警告:如把平台分销关闭,系统会把平台下的商品及商家状态都改为【关闭分销】",
                data: param
            });
        }
    }

    $scope.selectRebate = function(isOpenRebate){
        if(isOpenRebate == 0){
             var param = {
                isOpenRebate: isOpenRebate
             };
             // query data.
             utilService.modal($modal, $scope, {
                 title: "警告",
                 controller: "updateSellCtrl",
                 message: "警告:如把平台排队返利关闭,系统会把平台下的商品及商家状态都改为【关闭返利】",
                 data: param
             });
         }
    }
}]);

app.controller('updateSellCtrl', ['$scope', '$modalInstance','modalData',
    function ($scope, $modalInstance,modalData) {
        $scope.modal = modalData;
        $scope.systemConfig = {
            id: modalData.data.id,
            isOpenSell: modalData.data.isOpenRebate
        };
        $scope.ok = function () {
            $scope.cancel();
        }
        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    }]);

app.controller('updateUserStatusCtrl', ['$scope', '$modalInstance','$state','merchantsService','toaster','data',
    function($scope, $modalInstance,$state,merchantsService,toaster,data) {
        $scope.data=data;
        $scope.AllowToDelete=true;

        $scope.ok=function(){
            if(!$scope.AllowToDelete){
                return;
            }
            $scope.AllowToDelete=false;

            merchantsService.updateSystemUserStatus($scope).then(function(result){
                switch(result.status){
                    case 200:
                        $modalInstance.dismiss('success');
                        toaster.pop('success', "提示", "操作成功");
                        $scope.query();
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break;
                }
            });
        };

        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
}]);

//审核通过弹窗
app.controller('merchantsAuditCtrl', ['$scope', '$modalInstance','$state','merchantsService','toaster','modalData',
    function($scope, $modalInstance,$state,merchantsService,toaster,modalData) {
        $scope.modal = modalData;
        $scope.merchants = {
            id: modalData.data.id,
            auditStatus:modalData.data.auditStatus
        };
        $scope.AllowToDelete=true;

        $scope.ok=function(){
            if(!$scope.AllowToDelete){
                return;
            }
            $scope.AllowToDelete=false;
            merchantsService.updateMerchantsAudit($scope).then(function(result){
                switch(result.status){
                    case 200:
                        $modalInstance.dismiss('success');
                        toaster.pop('success', "提示", "操作成功");
                        $scope.query();
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break;
                }
            });
        }

        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
}]);

app.controller('infoCtrl', ['$scope', '$modalInstance','$state','merchantsService','toaster','modalData',
    function($scope, $modalInstance,$state,merchantsService,toaster,modalData) {
        $scope.modal = modalData;
        $scope.merchants = {
            id: modalData.data.id
        };
        $scope.ok=function(){
            $state.go('app.merchantsEdit',{shopId:$scope.merchants.id});
            $modalInstance.dismiss('success');
        }

        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    }]);

//审核不通过
app.controller('shopAuditFailCtrl',['$scope','$stateParams','merchantsService','toaster','$state', function($scope,$stateParams,merchantsService,toaster,$state){
    $scope.merchants= {
        id:$stateParams.id,
        auditStatus:1
    };

    $scope.confirm=function(){
        //console.info( $scope.merchants)
        merchantsService.updateMerchantsAudit($scope).then(function(result){
            switch(result.status){
                case 200:
                    toaster.pop('success', "提示", "操作成功");
                    $state.go("app.merchants");
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    }

}]);